<template>
  <view class="normal-login-container">
    <view class="logo-content align-center justify-center flex">
      <image style="width: 100rpx;height: 100rpx;" :src="baseUrl + '/profile/upload/mini/23.png'" mode="widthFix">
      </image>
      <text class="title">鲁能光大</text>
    </view>
    <view class="login-form-content">
      <view class="input-item flex align-center">
        <view class="iconfont icon-user icon"></view>
        <input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
      </view>
      <view class="input-item flex align-center">
        <view class="iconfont icon-password icon"></view>
        <input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
      </view>
	  <view class="remember-password">
	    <checkbox-group @change="rememberPasswordChange">
	      <label>
	        <checkbox value="remember"  color="#007aff" style="transform:scale(0.7)" :checked="rememberPassword" /><text style="margin-left: 15rpx;">记住密码</text>
	      </label>
	    </checkbox-group>
	  </view>
      <view class="action-btn">
        <button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
      </view>
    </view>
     
  </view>
</template>

<script>
  import * as index from '@/api/index.js'
  export default {
    data() {
      return {
        baseUrl:this.$tools.baseURL,
        codeUrl: "",
        captchaEnabled: true,
        // 用户注册开关
        register: false,
        globalConfig: getApp().globalData.config,
        loginForm: {
          username: "",
          password: "",
          code: "",
          uuid: ''
        },
		 rememberPassword: false
      }
    },
    created() {
      // this.getCode()
	  this.loadRememberedCredentials()
    },
	onShow() {
		// 判断本地存储中是否存在 App-Token
		// this.getInfo()
	},
    methods: {
      // 登录方法
      async handleLogin() {
        if (this.loginForm.username === "") {
          this.$tools.toast("请输入您的账号")
        } else if (this.loginForm.password === "") {
          this.$tools.toast("请输入您的密码")
        } else {
			index.login(this.loginForm).then(res=>{
				console.log('登录成功了吗')
				if (this.rememberPassword) {
				  this.saveCredentials()
				} else {
				  this.clearCredentials()
				}
				uni.setStorageSync('App-Token', res.token)
				// 获取用户信息
				this.getInfo()
			})
          
        }
      },
	  getInfo() {
		 // 设置用户信息
		 this.$store.dispatch('GetInfo').then(res => {
		   this.$tab.reLaunch('/pages/home/index')
		 })
	  },
	  // 记住密码复选框状态改变
	  rememberPasswordChange(e) {
	    this.rememberPassword = e.detail.value.includes('remember')
	  },
	  // 保存账号密码
	  saveCredentials() {
	    uni.setStorageSync('username', this.loginForm.username)
	    uni.setStorageSync('password', this.loginForm.password)
	  },
	  // 清除账号密码
	  clearCredentials() {
	    uni.removeStorageSync('username')
	    uni.removeStorageSync('password')
	  },
	  // 加载记住的账号密码
	  loadRememberedCredentials() {
	    const username = uni.getStorageSync('username')
	    const password = uni.getStorageSync('password')
	    if (username && password) {
	      this.loginForm.username = username
	      this.loginForm.password = password
	      this.rememberPassword = true
	    }
	  }
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .normal-login-container {
    width: 100%;

    .logo-content {
      width: 100%;
      font-size: 21px;
      text-align: center;
      padding-top: 15%;

      image {
        border-radius: 4px;
      }

      .title {
        margin-left: 10px;
      }
    }

    .login-form-content {
      text-align: center;
      margin: 20px auto;
      margin-top: 15%;
      width: 80%;

      .input-item {
        margin: 20px auto;
        background-color: #f5f6f7;
        height: 45px;
        border-radius: 20px;

        .icon {
          font-size: 38rpx;
          margin-left: 10px;
          color: #999;
        }

        .input {
          width: 100%;
          font-size: 14px;
          line-height: 20px;
          text-align: left;
          padding-left: 15px;
        }

      }

      .login-btn {
        margin-top: 40px;
        height: 45px;
      }
      
      .reg {
        margin-top: 15px;
      }
      
      .xieyi {
        color: #333;
        margin-top: 20px;
      }
      
      .login-code {
        height: 38px;
        float: right;
      
        .login-code-img {
          height: 38px;
          position: absolute;
          margin-left: 10px;
          width: 200rpx;
        }
      }
	  .remember-password {
		display: flex;
		align-items: center;
		  margin-top: 10px;
		  text-align: left;
		  padding-left: 20px;
		}
    }
  }

</style>
